package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.CommentListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论——编辑界面
 */
public class EditCommentActivity extends BaseActivity implements View.OnClickListener,TextWatcher {
    private EditText etContent;
    private TextView tvLength;//内容长度
    private TextView btnSubmit;//发布
    private String mVedioId;//视频id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_comment);
        setTitle("写评论", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle != null){
            mVedioId = bundle.getString("id");
        }

        etContent = (EditText) findViewById(R.id.et_remarks);
        tvLength = (TextView) findViewById(R.id.tv_length);
        btnSubmit = (TextView) findViewById(R.id.tv_publish);

        btnSubmit.setOnClickListener(this);
        etContent.addTextChangedListener(this);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() > 200) {
            tvLength.setTextColor(getResources().getColor(R.color.colorB93A3C));
            btnSubmit.setSelected(true);
            btnSubmit.setClickable(false);
        } else {
            tvLength.setTextColor(getResources().getColor(R.color.color999999));
            btnSubmit.setSelected(false);
            btnSubmit.setClickable(true);
        }
        tvLength.setText(String.format(getResources().getString(R.string.input_length_200), String.valueOf(s.length())));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_publish:
                String content = etContent.getText().toString().trim();
                if(StringUtils.isEmpty(content)){
                    ToastUtil.makeLongText(this,getString(R.string.input_comment));
                    return;
                }

                loadWaitDialog();
//                HttpMethod.handpicAddLeave(this,this,mVedioId,content);
                HttpMethod.kojiComment(this,this,content,mVedioId,"3");
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if(method == HttpMethod.HTTP_KOJI_COMMENT){
            List<CommentListBean.CommentBean> dataList = new ArrayList<>();
            CommentListBean.CommentBean commentBean = new CommentListBean.CommentBean();
            commentBean.setAddtime("刚刚");
            String content = etContent.getText().toString().trim();
            commentBean.setContent(content);
            commentBean.setUsername(MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_NAME));
            commentBean.setUserpic(MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_HEAD));
            dataList.add(commentBean);

            Intent intent = new Intent();
            intent.putExtra("comment_data", (Serializable) dataList);
            setResult(RESULT_OK,intent);

            myFinish();
        }
    }

    @Override
    protected void myToast() {
        super.myToast();
        ToastUtil.makeLongText(this, "发布成功");
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if(!StringUtils.isEmpty(message)){
            ToastUtil.makeLongText(this,message);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override
    public void afterTextChanged(Editable s) {}
}
